import React, { Component } from 'react';
import { Link, Redirect } from 'react-router-dom';
import { Button, Card, CardBody, CardGroup, Col, Container, Form, Input, InputGroup, InputGroupAddon, InputGroupText, Row } from 'reactstrap';


export class SignIn extends Component {

    constructor(props) {
        super(props)
        const token = sessionStorage.getItem("token") //get token from local storage 
        let loggedIn = true

        if (token == null) {
            loggedIn = false
        }

        this.state = {
            Email: '',
            Password: '',
            loggedIn
        }
        this.Password = this.Password.bind(this);
        this.Email = this.Email.bind(this);
        this.login = this.login.bind(this);
    }
 
    Email(event)
    {
        this.setState({ Email: event.target.value })
    }
    Password(event)
    {
        this.setState({ Password: event.target.value })
    }

    login(event) {
        event.preventDefault();
        fetch(process.env.REACT_APP_API_LOGIN + 'Login' +'/LoginUser', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            credentials: 'include',

            body: JSON.stringify({
                Email: this.state.Email,
                Password: this.state.Password
            })
        })
            .then((Response) => Response.json())
            .then((result) => {
                if (result == "Success")
                {
                    alert('Login Successfully');
                    sessionStorage.setItem("token","sqhfuwqghfhipwqhhih")
                    this.setState({
                        loggedIn: true
                    })
                   this.props.history.push("/admin/adminhome");
                }
                else
                {
                    alert('Invelid user ');
                }
            },
            (error) => {
                alert(error);

            })

    }  

    render() {
        if (this.state.loggedIn)
        {
            return <Redirect to="/admin/adminhome"/>
        }
        return (
            <div className="app flex-row align-items-center">
                <Container>
                    <Row className="justify-content-center">
                        <Col md="9" lg="7" xl="6">
                            <CardGroup>
                                <Card className="p-2">
                                    <CardBody>
                                        <Form onSubmit={this.login}>
                                            <div className="row" className="mb-2 pageheading">
                                                <div className="col-sm-12 btn btn-primary">
                                                    Login
                                                </div>
                                            </div>
                                            <InputGroup className="mb-3">
                                                <Input type="text" onChange={this.Email} placeholder="Enter Email" />
                                            </InputGroup>
                                            <InputGroup className="mb-4">
                                                <Input type="password" onChange={this.Password} placeholder="Enter Password" />
                                            </InputGroup>
                                            <Button color="success" type="submit" block > Login</Button>
                                        </Form>
                                    </CardBody>
                                </Card>
                            </CardGroup>
                        </Col>
                    </Row>
                </Container>
            </div>
        );
    }

}